package com.csii.branch.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csii.core.util.ConvertUtils;
import com.csii.core.api.Result;
import com.csii.core.system.query.QueryGenerator;
import com.csii.core.util.oConvertUtils;
import com.csii.branch.mapper.entity.CdBranchStatDevice;
import com.csii.branch.service.ICdBranchStatDeviceService;
import com.csii.branch.controller.vo.CdBranchStatDeviceVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import com.csii.core.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.csii.core.annotation.AutoLog;

 /**
 * @Description: 网点设备统计信息
 * @Author: power-stdio
 * @Date:   2020-12-23
 * @Version: V1.0
 */
@Api(tags="网点设备统计信息")
@RestController
@RequestMapping("/cdBranchStatDevice")
@Slf4j
public class CdBranchStatDeviceController extends BaseController<CdBranchStatDevice, ICdBranchStatDeviceService> {
	@Autowired
	private ICdBranchStatDeviceService cdBranchStatDeviceService;
	@ApiOperation(value="根据网点设备统计信息", notes="根据网点设备统计信息")
	@GetMapping(value = "/statByNetId")
	public Result<?> statByNetId(@RequestParam(name="netId") String netId){
		return Result.ok(ConvertUtils.convertList(CdBranchStatDeviceVO.class, cdBranchStatDeviceService.statByNetId(netId)));
	}
	/**
	 * 分页列表查询
	 *
	 * @param cdBranchStatDeviceVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-分页列表查询")
	@ApiOperation(value="网点设备统计信息-分页列表查询", notes="网点设备统计信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CdBranchStatDeviceVO cdBranchStatDeviceVO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		CdBranchStatDevice cdBranchStatDevice  = ConvertUtils.convert(CdBranchStatDevice.class,cdBranchStatDeviceVO);
		QueryWrapper<CdBranchStatDevice> queryWrapper = QueryGenerator.initQueryWrapper(cdBranchStatDevice, req.getParameterMap());
		Page<CdBranchStatDevice> page = new Page<CdBranchStatDevice>(pageNo, pageSize);
		IPage<CdBranchStatDevice> pageList = cdBranchStatDeviceService.page(page, queryWrapper);
		IPage<CdBranchStatDeviceVO> pageListVo = ConvertUtils.convertIPage(CdBranchStatDeviceVO.class, pageList);
		return Result.ok(pageListVo);
	}
	
	/**
	 *   添加
	 *
	 * @param cdBranchStatDeviceVO
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-添加")
	@ApiOperation(value="网点设备统计信息-添加", notes="网点设备统计信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CdBranchStatDeviceVO cdBranchStatDeviceVO) {
		CdBranchStatDevice cdBranchStatDevice  = ConvertUtils.convert(CdBranchStatDevice.class,cdBranchStatDeviceVO);
		cdBranchStatDeviceService.save(cdBranchStatDevice);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cdBranchStatDeviceVO
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-编辑")
	@ApiOperation(value="网点设备统计信息-编辑", notes="网点设备统计信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CdBranchStatDeviceVO cdBranchStatDeviceVO) {
		CdBranchStatDevice cdBranchStatDevice  = ConvertUtils.convert(CdBranchStatDevice.class,cdBranchStatDeviceVO);
		cdBranchStatDeviceService.updateById(cdBranchStatDevice);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-通过id删除")
	@ApiOperation(value="网点设备统计信息-通过id删除", notes="网点设备统计信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		cdBranchStatDeviceService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-批量删除")
	@ApiOperation(value="网点设备统计信息-批量删除", notes="网点设备统计信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cdBranchStatDeviceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "网点设备统计信息-通过id查询")
	@ApiOperation(value="网点设备统计信息-通过id查询", notes="网点设备统计信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CdBranchStatDevice cdBranchStatDevice = cdBranchStatDeviceService.getById(id);
		if(cdBranchStatDevice==null) {
			return Result.error("未找到对应数据");
		}
		CdBranchStatDeviceVO cdBranchStatDeviceVO  = ConvertUtils.convert(CdBranchStatDeviceVO.class,cdBranchStatDevice);
		return Result.ok(cdBranchStatDeviceVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cdBranchStatDeviceVO
    */
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CdBranchStatDeviceVO cdBranchStatDeviceVO) {
        CdBranchStatDevice cdBranchStatDevice  = ConvertUtils.convert(CdBranchStatDevice.class,cdBranchStatDeviceVO);
        return super.exportXls(request, cdBranchStatDevice, CdBranchStatDevice.class, "网点设备统计信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CdBranchStatDevice.class);
    }
}
